import requests
from lxml import etree
#创建列表
data=[]
for i in range(1,7):
    resp=requests.get(f'https://newhouse.fang.com/house/s/b9{i}/')
    resp.encoding='utf-8'
    
    #解析数据
    e=etree.HTML(resp.text)
    names=e.xpath('//div[@class="nlcd_name"]/a/text()')
    adreses=e.xpath('//div[@class="address"]/a/@title')
    # 把div[@class="nhouse_price"]下的span和em都遍历提出来('string(.)并去掉空格.strip()
    prices=[d.xpath('string(.)').strip() for d in e.xpath('//div[@class="nhouse_price"]')]
    tel=e.xpath('//div[@class="tel"]/p/text()')
    
    #处理数据
    # data=[]
    for n,a,p,t in zip(names,adreses,prices,tel):
        data.append([n,a,p,t])
import pandas
#按表格方式输出
df=pandas.DataFrame(data,columns=['小区名','地址','价格','电话'])
print(df)

    
